package csu.edu.flatprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Activity;
import csu.edu.flatprogram.entity.Picture;
import csu.edu.flatprogram.persistence.ActivityMapper;
import csu.edu.flatprogram.persistence.PictureMapper;
import csu.edu.flatprogram.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("activityService")
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private PictureMapper pictureMapper;

    //查询所有活动信息
    @Override
    public CommonResponse<List<Activity>> getAllActivities() {
       List<Activity> activityList = activityMapper.selectList(null);
       if(activityList.isEmpty()){
           return CommonResponse.createForSuccessMessage("目前没有活动信息");
       }
       return CommonResponse.createForSuccess(activityList);
    }

    //根据ID查询单条活动信息
    @Override
    public CommonResponse<Activity> getActivityById(int activityid) {
        Activity activity = activityMapper.selectById(activityid);
        if(activity == null){
            return CommonResponse.createForSuccessMessage("没有该ID的活动信息");
        }
        return CommonResponse.createForSuccess(activity);
    }

    //新增一条活动
    @Override
    public CommonResponse<Activity> addActivity(String activityname, String starttime, String stoptime, String activityplace, String activityunit, String activitydetails) {
        Activity activity = new Activity();
        activity.setActivityname(activityname);
        activity.setStarttime(starttime);
        activity.setStoptime(stoptime);
        activity.setActivityplace(activityplace);
        activity.setActivityunit(activityunit);
        activity.setActivitydetails(activitydetails);
        int i = activityMapper.insert(activity);
        return CommonResponse.createForSuccess("已新增了"+activityname+"活动通知信息",activity);
    }

    //更新一条活动
    @Override
    public CommonResponse<Activity> updateActivity(int activityid,String activityname, String starttime, String stoptime, String activityplace, String activityunit, String activitydetails) {
        Activity activity = activityMapper.selectById(activityid);
        activity.setActivityname(activityname);
        activity.setStarttime(starttime);
        activity.setStoptime(stoptime);
        activity.setActivityplace(activityplace);
        activity.setActivityunit(activityunit);
        activity.setActivitydetails(activitydetails);
        int i = activityMapper.updateById(activity);
        return CommonResponse.createForSuccess("已修改了" + activityname + "活动通知信息", activity);
    }


    //通过ID删除一条活动通知
    @Override
    public CommonResponse<List<Activity>> deleteActivity(String activityid) {
        int i = activityMapper.deleteById(activityid);
        return CommonResponse.createForSuccessMessage("删除成功");
    }

    //添加活动post
    @Override
    public CommonResponse<Activity> insertActivity(Activity activity) {
        Activity activity1 = new Activity();
        activity1.setActivityname(activity.getActivityname());
        activity1.setStarttime(activity.getStarttime());
        activity1.setStoptime(activity.getStoptime());
        activity1.setActivityplace(activity.getActivityplace());
        activity1.setActivityunit(activity.getActivityunit());
        activity1.setActivitydetails(activity.getActivitydetails());
        activity1.setActivitypicture(activity.getActivitypicture());
        int i = activityMapper.insert(activity1);
        if(i == 0){
            return CommonResponse.createForSuccessMessage("添加失败");
        }
        return CommonResponse.createForSuccess("添加成功",activity);
    }

    //更新活动post
    @Override
    public CommonResponse<Activity> updateActivity(Activity activity) {
        int i = activityMapper.selectCount(Wrappers.<Activity>query().eq("activityid",activity.getActivityid()));
        if(i == 0){
            return CommonResponse.createForError("活动ID不存在");
        }
        Activity activity1 = new Activity();
        activity1.setActivityid(activity.getActivityid());
        activity1.setActivityname(activity.getActivityname());
        activity1.setStarttime(activity.getStarttime());
        activity1.setStoptime(activity.getStoptime());
        activity1.setActivityplace(activity.getActivityplace());
        activity1.setActivityunit(activity.getActivityunit());
        activity1.setActivitydetails(activity.getActivitydetails());
        activity1.setActivitypicture(activity.getActivitypicture());
        i = activityMapper.update(activity1,Wrappers.<Activity>query().eq("activityid",activity.getActivityid()));
        if(i == 0){
            return CommonResponse.createForSuccessMessage("更新失败");
        }
        return CommonResponse.createForSuccess("更新成功",activity);
    }

    @Override
    public CommonResponse<Picture> addPicture(Picture picture) {
        Picture picture1 = new Picture();
        picture1.setPicture(picture.getPicture());
        int i = pictureMapper.insert(picture1);
        if(i == 0){
            return CommonResponse.createForSuccessMessage("添加失败");
        }
        return CommonResponse.createForSuccess("添加成功",picture1);
    }
}
